import moment from 'moment'
import store from '@/store/index'

export const dateFormat = (date) => {
  return moment(date).format('MM-DD')
}

export const timeFormat = (time) => {
  return moment(time).format('HH:mm')
}

export const buildDisplayTime = (date, time) => {
  let timestamp

  const waitFormat = moment(date + ' ' + time)

  const diffDay = moment().diff(waitFormat, 'days')
  if (diffDay === 0) {
    timestamp = waitFormat.format('HH:mm')
  } else if (diffDay === 1) {
    timestamp = '昨天' + waitFormat.format('HH:mm')
  } else if (diffDay > 1 && diffDay < 365) {
    timestamp = waitFormat.format('MM-DD')
  } else {
    timestamp = waitFormat.format('YYYY-MM-DD')
  }

  return timestamp
}

export const buildDisplayTimeRoom = (date, time) => {
  let timestamp
  const waitFormat = moment(date + ' ' + time)
  const diffDay = moment().diff(waitFormat, 'days')
  if (diffDay === 0) {
    timestamp = waitFormat.format('HH:mm')
  } else if (diffDay === 1) {
    timestamp = '昨天' + waitFormat.format('HH:mm')
  } else if (diffDay > 1 && diffDay < 365) {
    timestamp = waitFormat.format('MM-DD HH:mm')
  } else {
    timestamp = waitFormat.format('YYYY-MM-DD HH:mm')
  }

  return timestamp
}

export const buildI18nTime = (time, isSecond = true) => {
  var lang = uni.getLocale()
  if (lang !== 'zh-Hans') {
    time = isSecond
      ? moment(time).format('DD.MM.YYYY HH:mm:ss')
      : moment(time).format('DD.MM.YYYY')
  } else {
    time = isSecond
      ? moment(time).format('YYYY.MM.DD HH:mm:ss')
      : moment(time).format('YYYY.MM.DD')
  }
  return time
}

export const buildI18nFormatTime = (time, isSecond = true) => {
  var lang = uni.getLocale()
  if (lang !== 'zh-Hans') {
    time = isSecond
      ? moment(time).format('DD/MM/YYYY HH:mm:ss')
      : moment(time).format('DD/MM/YYYY')
  } else {
    time = isSecond
      ? moment(time).format('YYYY/MM/DD HH:mm:ss')
      : moment(time).format('YYYY/MM/DD')
  }
  return time
}

export const voiceFormatTime = (seconds) => {
  const myMinutes = parseInt(seconds / 60) || 0
  const mySeconds = seconds % 60
  return `${myMinutes}′${mySeconds}″`
}
